<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="/erp/css/style.css" type="text/css"/>
    <script type="text/javascript" src="/erp/js/jquery.2.1.4.js"></script>
    <script>
        var ajax = 1;

        function del(t) {
            $(t).parent().parent().remove();
            var sum = 0;
            $(".sum").each(function () {
                var n = parseFloat($(this).val());
                sum = sum + n;
            });
            $("#count").html(sum);
        }

        function con(th) {
            $("#tab2").hide();
            $("#tab1").show();
            $("#oper").show();
            var gname = $(th).parent().next().text();
            var unit = $(th).parent().next().next().text();
            var valnum = $(th).parent().prev().val();
            $(".flag").val(gname);
            $(".flag").parent().next().next().children().val(unit);
            $(".flag").parent().next().val(valnum);
        }

        function select(th) {
            $("#tab1").hide();
            $("#oper").hide();
            $("#tab2").show();
            $(".flag").removeClass("flag");
            $(th).children().addClass("flag");
            if (ajax == 1) {
                $.post("/erp/outstoreCtrl?parm=select", function (data) {
                    for (var goods in data) {
                        $("#goods").after('<tr>\n' +
                            '           <input type="hidden" name="valmun" value="' + data[goods].gid + '">\n' +
                            '<td><a class="button white" href="javascript:void(0);" onclick="con(this);">选择</a>\n ' +
                            '            <td>' + data[goods].gname + '</td>\n' +
                            '            <td>' + data[goods].unit + '</td>\n' +
                            '            <td>' + data[goods].merc + '</td>\n' +
                            '            <td>' + data[goods].place + '</td>\n' +
                            '            <td>' + data[goods].inprice + '</td>\n' +
                            '        </tr>');
                    }

                }, "json");
                ajax = 0;
            }
        }

        function add() {
            $("#title").after('<tr class="items">\n' +
                '        <td class="gname" onclick="select(this);" style="width:30%"><input type="text" style="width:100%;height:30px;border: 0px;padding:0px" "/></td>\n' +
                '        <input class="ggid" type="hidden" name="valnum"  >\n' +
                '        <td class="ggunit"><input type="text" class="gunit" style="width:100%;height:30px;border: 0px;"></td>\n' +
                '        <td class="gnum"><input type="text" class="num" style="width:100%;height:30px;border: 0px;"></td>\n' +
                '        <td class="gprice"><input type="text" class="price" style="width:100%;height:30px;border: 0px;"></td>\n' +
                '        <td class="gsum"><input type="text" class="sum" style="width:100%;height:30px;border: 0px;"></td>\n' +
                '        <td><a class="button white" href="javascript:void(0);" onclick="del(this);">删除</a></td>\n' +
                '    </tr>');
            $(".num").keyup(function () {
                var num = $(this).val().trim();
                var price = $(this).parent().next().children().val().trim();
                if (num != '' && price != '') {
                    $(this).parent().next().next().children().val(num * price);
                    var sum = 0;
                    $(".sum").each(function () {
                        var n = parseFloat($(this).val());
                        sum = sum + n;
                    });
                    $("#count").html(sum);
                }
            });
            $(".price").keyup(function () {
                var num = $(this).parent().prev().children().val().trim();
                var price = $(this).val().trim();
                if (num != '' && price != '') {
                    $(this).parent().next().children().val(num * price);
                    var sum = 0;
                    $(".sum").each(function () {
                        var n = parseFloat($(this).val());
                        sum = sum + n;
                    });
                    $("#count").html(sum);
                }
            });
            $(".sum").keyup(function () {
                var sum = 0;
                $(".sum").each(function () {
                    var n = parseFloat($(this).val());
                    sum = sum + n;
                });
                $("#count").html(sum);
            });
        }

        function valid() {
            var no = $("#no").html();
            var hand = $("#hand").val();
            var date = $("#date").html();
            var maker = $("#maker").val();
            var keeper = $("#keeper").val();
            var remark = $("#remark").val();
            var count = $("#count").html();
            var regid = ",";
            var error = 0;

            if (hand == "") {
                alert("交货单位不能为空");
                return 1;
            }
            if (maker == "") {
                alert("制单人不能为空");
                return 1;
            }
            if (keeper == "") {
                alert("保管员不能为空");
                return 1;
            }


            $(".items").each(function (i, dom) {
                var num = $(dom).children(".gnum").children().val();
                var price = $(dom).children(".gprice").children().val();
                var gid = $(dom).children(".ggid").val();
                if (gid == "") {
                    alert("不能有空商品！");
                    error = 1;
                    return;
                }
                if (num == "" || price == "") {
                    alert("数量或单价不能为空");
                    error = 1;
                    return;
                }
                var sp = regid.split(",");
                for (var i = 0; i < sp.length; i++) {
                    if (gid == sp[i]) {
                        alert("不能有重复商品");
                        error = 1;
                        return;
                    }
                }
                regid = regid + gid + ",";
            });
            return error;
        }

        function instore() {
            var ret = valid();
            if (ret == 1) {
                return;
            }

            var no = $("#no").html();
            var hand = $("#hand").val();
            var date = $("#date").html().trim();
            var maker = $("#maker").val();
            var keeper = $("#keeper").val();
            var remark = $("#remark").val();
            var count = $("#count").html();

            $(".items").each(function (i, dom) {
                var gid = $(dom).children(".ggid").val();
                var gname = $(dom).children(".gname").children().val();
                var unit = $(dom).children(".ggunit").children().val();
                var num = $(dom).children(".gnum").children().val();
                var price = $(dom).children(".gprice").children().val();
                var sum = $(dom).children(".gsum").children().val();

                $.post("/erp/instoreCtrl?parm=plus", {num: num, gid: gid});
                $.post("/erp/instoreCtrl?parm=add",
                    {
                        gid: gid,
                        gname: gname,
                        unit: unit,
                        num: num,
                        price: price,
                        sum: sum,
                        no: no,
                        hand: hand,
                        date: date,
                        maker: maker,
                        keeper: keeper,
                        remark: remark,
                        count: count
                    });
            });
            setTimeout("window.location.href='/erp/instoreCtrl?parm=list';" ,500);
        }
    </script>
</head>
<body>
<div align="center" style="width: 100%;margin-bottom:20px"><h2 style="display: inline">入&nbsp;&nbsp;&nbsp;库&nbsp;&nbsp;&nbsp;单</h2>
    <span style="float: right"><span>No:</span><span id="no"><%=new Date().getTime() %></span></span></div>
<div align="cetner" style="width: 100%">
    <span style="float: left">交货单位 (人):
        <input id="hand" type="text"
               style="border-left:0px;border-top:0px;border-right:0px;border-bottom:2px"/></span><span id="date" style="float: right"><fmt:formatDate value="<%=new Date() %>" type="date" dateStyle="long"/></span></div>
<table id="tab1" border="1px" align="center" cellspacing="0" cellpadding="0" width="100%" table-layout:fixed>
    <tr id="title">
        <th style="width:30%">货 名</th>
        <th style="width:15%">单位</th>
        <th style="width:15%">数量</th>
        <th style="width:15%">单价(元)</th>
        <th style="width:15%">金额(元)</th>
        <th style="width:10%"></th>
    </tr>

    <tr>
        <td>备 注</td>
        <td colspan="5"><input id="remark" type="text" style="width:100%;height:30px;border: 0px;margin: 0px"></td>
    </tr>

    <td>合计:</td>
    <td colspan="5"><span style="float: left;margin-left: 30px"><span>￥</span><span id="count"></span><span>（元）</span></span></td>
    </tr>
</table>
<div style="width: 100%">
    <table style="display: none;" id="tab2" border="1px" align="center" cellspacing="0" cellpadding="0" width="100%"
           table-layout:fixed>
        <caption>请选择商品</caption>
        <tr id="goods">
            <th style="width: 10%"></th>
            <th style="width: 30%">商品名称</th>
            <th style="width: 10%">单位</th>
            <th style="width: 20%">供应商</th>
            <th style="width: 20%">商品产地</th>
            <th style="width: 10%">进价</th>
        </tr>
    </table>
</div>
<div style="width: 100%"><span style="float: left">保管员:
    <input id="keeper" type="text" style="border-left:0px;border-top:0px;border-right:0px;border-bottom:1px"/></span>
    <span style="float: right">制单人:
        <input id="maker" type="text" style="border-left:0px;border-top:0px;border-right:0px;border-bottom:1px"/></span>
</div>
<div id="oper" style="text-align: center;margin-top: 35px">
    <a class="button blue" href="javascript:void(0);" onclick="add();">添加货物</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <a class="button blue" href="javascript:void(0);" onclick="instore();">保存入库</a>
</div>
</body>
</html>